package com.shuwang.jh_shop.ui.base

import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast

/**
 * 作者  Administrator
 * 时间  2016/10/10 14:12
 * 文件  oilapp
 * 描述  处理FragmentTabHost缓存问题
 */

abstract class BaseFragment : Fragment() {
    protected var inflater: LayoutInflater? = null
    private var contentView: View? = null
    private var container: ViewGroup? = null
    protected var applicationContext: Context? = null

    override fun onAttach(context: Context?) {
        super.onAttach(context)
        this.applicationContext = context
    }

    /**
     * onCreateView的生命周期是怎样的
     */
    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        this.inflater = inflater
        this.container = container
        if (contentView == null) {
            setContentView()
        } else {
            var parent = contentView?.parent
            if (parent!=null) {
                parent = parent as ViewGroup
                parent.removeView(contentView)
            }
        }
        return contentView
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

    }

    abstract fun setContentView()

    fun setContentView(layoutResID: Int) {
        setContentView(inflater?.inflate(layoutResID, container, false) as View)
    }

    fun setContentView(view: View) {
        contentView = view
    }

    fun getContentView(): View {
        return contentView as View
    }


    fun showToast(msg: String) {
        Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show()
    }


}
